package com.solution.wx.bizman.customer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CustomerAccountEntity;
import com.solution.admin.customer.service.CustomerAccountService;
import com.solution.admin.finance.wallet.entity.WalletEntity;
import com.solution.admin.finance.wallet.service.WalletService;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.StringUtils;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户提现账号web控制层
 * @author lufer
 * @date 2018-05-04 16:53:12
 */
@RequestMapping("/wx/my/wxCustomerAccount")
@Controller
public class WxCustomerAccountController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(WxCustomerAccountController.class);

	@Autowired
	private CustomerAccountService customerAccountService;
	@Autowired
	private WalletService walletService;
	
	/**
	 * 跳转客户提现账号查询页面
	 * @param CustomerAccount 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lufer
 	 * @date 2018-05-04 16:53:12
	 */
	@RequestMapping("/toMyAccount")
	public String toMyAccount(HttpServletRequest request ) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//从session中获取到当前登录客户ID
		int customerId = (int) getCustomerId();
		//封装查询参数
		paramMap.put("customerId", customerId);
		//查询当前登录客户的财富
		List<CustomerAccountEntity> accountList = customerAccountService.listCustomerAccount(paramMap);
		if(null != accountList){
			request.setAttribute("accountList",accountList);
		}
		return "wx/my/wallet/account";
	}
	
	/**
	 * 跳转到提现账号选择界面
	 * 作者：lufer
	 * 时间：2018年6月4日 下午7:19:54
	 * @param request
	 * @return
	 */
	@RequestMapping("/toWithdrawAccount")
	public String toWithdrawAccount(HttpServletRequest request ) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//从session中获取到当前登录客户ID
		int customerId = (int) getCustomerId();
		//账号类型 bank银行卡 alipay支付宝
		String accountType = request.getParameter("accountType");
		//封装查询参数
		paramMap.put("customerId", customerId);
		paramMap.put("accountType", accountType);
		//查询当前登录客户的财富
		List<CustomerAccountEntity> accountList1 = customerAccountService.listCustomerAccount(paramMap);
		List<CustomerAccountEntity> accountList = new ArrayList<CustomerAccountEntity>();
		if(null != accountList1){
			Iterator<CustomerAccountEntity> it = accountList1.iterator();
			while(it.hasNext()){
				CustomerAccountEntity caEntity = (CustomerAccountEntity) it.next();
				if("bank".equals(caEntity.getAccountType())){
					String accountCode = StringUtils.substring(caEntity.getAccountCode(), caEntity.getAccountCode().length()-4, caEntity.getAccountCode().length());
					caEntity.setAccountCode(accountCode);
					accountList.add(caEntity);
				}else{
					String alipayAccount = StringUtils.substring(caEntity.getAlipayAccount(),0, 4);
					caEntity.setAlipayAccount(alipayAccount);
					accountList.add(caEntity);
				}
				
			}
			request.setAttribute("accountList",accountList);
		}
		request.setAttribute("accountType",accountType);
		return "wx/my/wallet/withdraw_account";
	}
	
	/**
	 * 根据选择的提现账号信息跳转到提现页面
	 * 作者：lufer
	 * 时间：2018年6月5日 下午4:09:20
	 * @param request
	 * @return
	 */
	@RequestMapping("/toWithdrawPage")
	public String toWithdrawPage(HttpServletRequest request ) {
		//从页面获取id
		long id = Long.valueOf(request.getParameter("id"));
		//查询当前登录客户的财富
		CustomerAccountEntity caEntity = customerAccountService.getCustomerAccountById(id);
		if(null != caEntity){
			if("bank".equals(caEntity.getAccountType())){
				String accountCode = StringUtils.substring(caEntity.getAccountCode(), caEntity.getAccountCode().length()-4, caEntity.getAccountCode().length());
				caEntity.setAccountCode(accountCode);
			}else{
				String alipayAccount = StringUtils.substring(caEntity.getAlipayAccount(),0, 3);
				caEntity.setAlipayAccount(alipayAccount);
			}
			request.setAttribute("caEntity",caEntity);
		}
		
		/***************查询当前登录客户财富汇总信息****************/
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//从session中获取到当前登录客户ID
		int customerId = (int) getCustomerId();
		//封装查询参数
		paramMap.put("customerId", customerId);
		//查询当前登录客户的财富
		List<WalletEntity> walletList = walletService.listWallet(paramMap);
		if(null != walletList){
			Iterator<WalletEntity> it = walletList.iterator();
			if(it.hasNext()){
				WalletEntity wallet = (WalletEntity)it.next();
				request.setAttribute("wallet", wallet);
			}
		}
		return "wx/my/wallet/withdraw_add";
	}

	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2018-05-04 16:53:12
	 */
	@RequestMapping("/toAddCustomerAccount")
	public String toAddCustomerAccount(HttpServletRequest request) {
		return "wx/my/wallet/account_add";
	}

	/**
	 * 新增或保存客户提现账号
	 * @param request 请求对象
	 * @param customerAccountEntity 客户提现账号实体
	 * @return
	 * @author lufer
 	 * @date 2018-05-04 16:53:12
	 */
	@RequestMapping("/saveOrUpdateCustomerAccount")
	@ResponseBody
	public Map<String, Object> saveCustomerAccount(HttpServletRequest request,CustomerAccountEntity customerAccountEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			
			//先根据提交的卡号或者支付宝账号查询是否已经存在
			if(StringUtils.isNotEmpty(customerAccountEntity.getAccountCode()) || StringUtils.isNotEmpty(customerAccountEntity.getAlipayAccount())){
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("accountCode", customerAccountEntity.getAccountCode());
				paramMap.put("alipayAccount", customerAccountEntity.getAlipayAccount());
				List<CustomerAccountEntity> accountList = customerAccountService.listCustomerAccount(paramMap);
				if(null != accountList){
					if(accountList.size()>0){
						resultMap.put("resultCode", 0);
						resultMap.put("resultMsg", "此账号已绑定不可重复绑定");
						return resultMap;
					}
				}
			}
			
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){//修改
				customerAccountEntity.setModifier(String.valueOf(getUserId()));//修改人
				result = customerAccountService.updateCustomerAccount(customerAccountEntity);
			}else{//新增
				customerAccountEntity.setCustomerId(getCustomerId());//所属用户
				customerAccountEntity.setCreater(String.valueOf(getUserId()));//创建人
				result = (int)customerAccountService.saveCustomerAccount(customerAccountEntity);
			}
			resultMap.put("resultCode", result);
			//执行结果
			if(result>0){
				resultMap.put("resultMsg", "保存成功");
			}else{
				resultMap.put("resultMsg", "保存失败");
			}
		} catch (Exception e) {
			resultMap.put("resultMsg", "保存异常");
			handleException("添加客户提现账号失败", logger, e);
		}
		//操作成功后跳转页面
		return resultMap;
	}
	
	/**
	 * 跳转解绑页面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2018-05-04 16:53:12
	 */
	@RequestMapping("/toDelCustomerAccount")
	public String toDelCustomerAccount(HttpServletRequest request) {
		// 编辑操作
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CustomerAccountEntity customerAccount = customerAccountService.getCustomerAccountById(id);
			request.setAttribute("account", customerAccount);
		} catch (Exception e) {
			handleException("跳转解绑客户提现账号页面错误", logger, e);
		}
		return "wx/my/wallet/account_del";
	}
	
	/**
	 * 解绑账号
	 * 作者：lufer
	 * 时间：2018年6月1日 下午2:48:31
	 * @param request
	 * @param customerAccountEntity
	 * @return
	 */
	@RequestMapping("/delCustomerAccount")
	@ResponseBody
	public Map<String, Object> delCustomerAccount(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			long id = Long.valueOf(request.getParameter("id"));
			result = customerAccountService.deleteCustomerAccountById(id);
			resultMap.put("resultCode", result);
			//执行结果
			if(result>0){
				resultMap.put("resultMsg", "解绑成功");
			}else{
				resultMap.put("resultMsg", "解绑失败");
			}
		} catch (Exception e) {
			resultMap.put("resultMsg", "解绑异常");
			handleException("解绑客户提现账号失败", logger, e);
		}
		//操作成功后跳转页面
		return resultMap;
	}
}
